package com.walkline.screen;

import java.util.Hashtable;
import java.util.Vector;

import localization.WeiboSDKResource;
import net.rim.device.api.i18n.ResourceBundle;
import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;

import com.walkline.util.Enumerations.RefreshAction;
import com.walkline.util.Enumerations.StatusCount;
import com.walkline.util.Function;
import com.walkline.util.container.ForegroundManager;
import com.walkline.util.container.MainMenuListStyleButtonSet;
import com.walkline.util.ui.MainMenuListStyleButtonField;
import com.walkline.weibo.WeiboSDK;
import com.walkline.weibo.inf.Tag;
import com.walkline.weibo.inf.TagsWrapper;

public class ShowFavoriteCatalogsMainScreen extends MainScreen implements WeiboSDKResource
{
	private static ResourceBundle _bundle = ResourceBundle.getBundle(BUNDLE_ID, BUNDLE_NAME);

	private WeiboSDK _weibo;

	private ForegroundManager _foreground=new ForegroundManager(0);
	private MainMenuListStyleButtonSet _listSet = new MainMenuListStyleButtonSet();
	private MainMenuListStyleButtonField _itemFavorites;
	private MainMenuListStyleButtonField[] _itemTags;

	private String _page_count = "20"; //WeiboSDK.STATUS_COUNT;
	private String _current_page = "1";
	private String _max_id = "0";

	public ShowFavoriteCatalogsMainScreen(WeiboSDK weibo)
	{
		super(NO_VERTICAL_SCROLL | USE_ALL_HEIGHT | NO_SYSTEM_MENU_ITEMS);

		_weibo = weibo;
		_page_count = StatusCount.choicesStatusCount[weibo.getAppConfig().getStatusCount()];

		HorizontalFieldManager hfm = new HorizontalFieldManager(FIELD_HCENTER);
		hfm.add(new LabelField("收藏分类"));
		setTitle(hfm);

		createUI();

		add(_foreground);
	}

	private void createUI()
	{
		String title = "所有收藏";
		_itemFavorites = new MainMenuListStyleButtonField(title);
		_itemFavorites.setChangeListener(new FieldChangeListener()
		{
			public void fieldChanged(Field field, int context)
			{
				Hashtable params = new Hashtable();

				params.put("title", _itemFavorites.getMenuTitle());
				params.put("count", _page_count);
				params.put("page", _current_page);

				UiApplication.getUiApplication().pushScreen(new ShowStatusesMainScreen(_weibo, params, RefreshAction.FAVORITES));
			}
		});
		_listSet.add(_itemFavorites);

		getFavoriteTags();

		_foreground.add(_listSet);
	}

	private void getFavoriteTags()
	{
		UiApplication.getUiApplication().invokeLater(new Runnable()
		{
			public void run()
			{
				Hashtable params = new Hashtable();

				params.put("count", _page_count);
				params.put("page", _current_page);

				RefreshContentsScreen popupScreen = new RefreshContentsScreen(_weibo, params, RefreshAction.FAVORITESTAGS);
				UiApplication.getUiApplication().pushModalScreen(popupScreen);

				TagsWrapper tagsWrapper = popupScreen.getTagsWrapper();

				if (popupScreen != null) {popupScreen = null;}
				if (tagsWrapper != null) {refreshTags(tagsWrapper);}				
			}
		});
	}

	private void refreshTags(TagsWrapper tagsWrapper)
	{
		Vector tags = tagsWrapper.getTags();

		if (tags.size() > 0)
		{
			_itemTags = new MainMenuListStyleButtonField[tags.size()];

			Tag tag;
			for (int i=0; i<tags.size(); i++)
			{
				tag = (Tag) tags.elementAt(i);
				_itemTags[i] = new MainMenuListStyleButtonField(tag);
				_itemTags[i].setChangeListener(new FieldChangeListener()
				{
					public void fieldChanged(Field field, int context)
					{
						MainMenuListStyleButtonField item = (MainMenuListStyleButtonField) _listSet.getFieldWithFocus();

						Hashtable params = new Hashtable();

						params.put("title", item.getMenuTitle());
						params.put("tid", item.getTag().getIdStr());
						params.put("count", _page_count);
						params.put("page", _current_page);

						UiApplication.getUiApplication().pushScreen(new ShowStatusesMainScreen(_weibo, params, RefreshAction.FAVORITESBYTAGS));
					}
				});
				_listSet.add(_itemTags[i]);
			}
		}
	}

	MenuItem menuRefreshMessage = new MenuItem(_bundle, MENU_REFRESH_MESSAGE, 100, 10)
    {
    	public void run() {}
    };

    //MenuItem menuExit = new MenuItem(_bundle, MENU_EXIT, 100, 80)
    //{
    //	public void run() {Function.showExitDialog();}
    //};

	protected void makeMenu(Menu menu, int instance)
	{
		//menu.add(menuRefreshMessage);
		//menu.addSeparator();
		//menu.add(menuExit);

		super.makeMenu(menu, instance);
	};

	protected boolean keyChar(char character, int status, int time)
    {
    	switch (character)
    	{
			case Characters.BACKSPACE:
				return true;
			case Characters.LATIN_CAPITAL_LETTER_R:
			case Characters.LATIN_SMALL_LETTER_R:
				return true;
			case Characters.LATIN_CAPITAL_LETTER_T:
			case Characters.LATIN_SMALL_LETTER_T:
				//if (_listSet.getFieldCount() > 0) {_listSet.getField(0).setFocus();}
				return true;
			case Characters.LATIN_CAPITAL_LETTER_B:
			case Characters.LATIN_SMALL_LETTER_B:
				//if (_listSet.getFieldCount() > 0) {_listSet.getField(_listSet.getFieldCount()-1).setFocus();}
				return true;
			case Characters.LATIN_CAPITAL_LETTER_E:
			case Characters.LATIN_SMALL_LETTER_E:
				return true;
			case Characters.ESCAPE:
				break;
			case Characters.LATIN_CAPITAL_LETTER_A:
			case Characters.LATIN_SMALL_LETTER_A:
				return true;
			case Characters.LATIN_CAPITAL_LETTER_Q:
			case Characters.LATIN_SMALL_LETTER_Q:
				Function.showExitDialog();
				return true;
		}

    	return super.keyChar(character, status, time);
    }
}